home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ccdl150l.zip / DATA / ATOF.C next >
C/C++ Source or Header  |  1996-06-24  |  999b  |  64 lines

  1. #include <stdlib.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4.  
  5. double strtod(const char *s, char **endptr)
  6. {
  7.     int sign = 0;
  8.     double frac = 0;
  9.     double val = 0;
  10.     int exp=0;
  11.  
  12.     while( isspace(*s)) s++;
  13.     if (*s == '-') {
  14.         sign++;
  15.         s++;
  16.     }
  17.     else if (*s == '+') 
  18.         s++;
  19.     if (*s == '.') {
  20.         frac = .1;
  21.         s++;                    
  22.     }
  23.     if (!frac) {
  24.         while(isdigit(*s)) {
  25.             val*= 10;
  26.             val += *s++-'0';
  27.         }
  28.         if (*s == '.') {
  29.             frac = .1;
  30.             s++;
  31.         }
  32.     }
  33.     if (frac)
  34.         while (isdigit(*s)) 
  35.             val += (*s++-'0')*frac;
  36.     if (sign)
  37.         val = - val;
  38.     sign = 0;
  39.     if (*s == 'e' || *s == 'E' || *s == 'd' || *s == 'E') {
  40.         s++;
  41.         if (*s == '-') {
  42.             sign++;
  43.             s--;
  44.         }
  45.         else if (*s == '+') 
  46.             s++;
  47.         while(isdigit(*s)) {
  48.             exp*= 10;
  49.             exp += *s++-'0';
  50.         }
  51.         if (exp > 1023)
  52.             exp = 1023;
  53.         if (sign)
  54.             exp = - exp;
  55.         val = val * pow(10.0,exp);
  56.     }
  57.     if (endptr)
  58.         *endptr = s;
  59.     return val;
  60. }
  61. double atof(const char *string)
  62. {
  63.     return(strtod(string,0));
  64. }